<script lang='ts' setup>
import { ref, reactive, toRefs, onBeforeMount, onMounted, watchEffect, computed, getCurrentInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Search } from '@element-plus/icons-vue'
const { proxy } = getCurrentInstance();
// proxy 替代 this
/**
* 路由对象
*/
const route = useRoute();
/**
* 路由实例
*/
const router = useRouter();

const data = reactive({
  input: "",
  isActive: false
})
onBeforeMount(() => {

})
onMounted(() => {
  const body = document.body
  const fancyInput = document.getElementsByClassName("fancy-input")[0]
  bindEvent(fancyInput, 'mousedown', (event: any) => {
    console.log(event)
    event.stopPropagation
      && event.stopPropagation();
    data.isActive = true
  })
  bindEvent(fancyInput, 'click', (event: any) => {
    console.log(event)
    event.stopPropagation
      && event.stopPropagation();
    data.isActive = true
  })
  bindEvent(body, 'click', function (event: any) {
    if (event.target.className == 'search-list') {
      return
    }
    data.isActive = false
  })
})
watchEffect(() => {
})
// 使用toRefs解构
// let { } = { ...toRefs(data) } 
defineExpose({
  ...toRefs(data)
})
function bindEvent(elem: Element, type: string, fn: { (event: any): void; (event: any): void; (event: any): void; }) {
  elem.addEventListener(type, fn)
}
const inp = ref<any>(null)
const props = defineProps({
  moduleList: {
    type: Object,
    default: []
  }
})
const showModules = computed(() => {
  return props.moduleList
})
//确认搜索
const checkedSearch = () => {
  console.log(data.input)
}
//
const scroll = (e: {
  target: {
    scrollHeight: any;
    clientHeight: any; scrollTop: any;
  };
}) => {
  // console.log(e.target.scrollTop, e.target.clientHeight, e.target.scrollHeight)
}

</script>

<template>
  <div class="second-meun">
    <!-- 二级菜单 -->
    <div class="child-meun">
      <div class="child" v-for="(item, index) in showModules" :key="index">
        <SvgIcon name="shouye" :size="20" />
        <span style="margin-left:10px;">{{ item.text }}</span>
      </div>
    </div>
    <!-- 主内容 -->
    <div class="right-content" @scroll="scroll">
      <div class="nav-title">
        <div>为您推荐精选频道</div>
        <div>音乐，游戏，校园，娱乐更多精彩频道等你发掘</div>
      </div>
      <div class="search">
        <el-input clearable class="fancy-input" :style="{
          backgroundColor: data.isActive ? 'rgba(255, 255, 255,1)' : 'rgba(255, 255, 255, .2)',
          maxWidth: data.isActive ? '700px' : '600px'
        }" v-model="data.input" ref="inp" placeholder="搜索 内容">
          <template #append>
            <el-button class="search-button" :icon="Search" @click="checkedSearch" />
          </template>
        </el-input>

        <!-- 搜索内容列表 9-->
        <div class="search-list" :style="{
          backgroundColor: data.isActive ? 'rgba(255, 255, 255,1)' : 'rgba(255, 255, 255, .2)',
          width: data.isActive ? '700px' : '600px',
          opacity: data.isActive ? '1' : '0'
        }">

        </div>
      </div>
      <RouterView></RouterView>
    </div>
  </div>
</template>

<style lang='scss'>
.second-meun {
  width: calc(100% - 100px);
  height: 100%;
  background:url("https://img0.baidu.com/it/u=2464806847,322937353&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500") no-repeat;
  background-size: 100% 100%;

  display: flex;

  .child-meun {
    width: 200px;
    height: 100%;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    overflow: auto;
    align-items: center;
    @include scroll-bar(6px, #ccc);

    .child {
      width: 180px;
      height: 50px;
      // background-color: orange;
      // margin: 10px 0;
      display: flex;
      align-items: center;
      border-radius: 10px;
      padding: 10px;
      box-sizing: border-box;
    }

    .child:hover {
      background-color: #f3f0f0;
    }

    .active {
      background-color: #f3f0f0;
    }
  }

  .right-content {
    width: calc(100% - 200px);
    height: 100%;
    overflow: hidden;
    overflow-y: auto;
    padding: 20px;
    box-sizing: border-box;
    @include scroll-bar(6px, #ccc);
    // background-color: orange;
    position: relative;

    .nav-title {
      width: 100%;
      height: 180px;
      // background-color: green;

    }

    .search {
      position: sticky;
      z-index: 999;
      top: -20px;
      left: 0px;
      height: 100px;
      width: 100%;
      // background-color: blue;

      .search-list {
        position: absolute;
        width: 600px;
        top: 60px;
        left: 0;
        z-index: 2;
        transition: all .5s ease;
        min-height: 100px;
        background: pink;
        border-radius: 10px;
        max-height: 400px;
      }

      .fancy-input {
        height: 50px;
        box-shadow: 0 0 0 .2px #fff inset;
        border-radius: 10px;
        padding: 0 10px;
        box-sizing: border-box;
        transition: all 0.5s ease;
        border: none;

        .el-input__wrapper {
          background: none;
          box-shadow: none;
          color: #000;

          .el-input__inner {
            color: #000;
            font-size: 20px;
          }
        }

        .el-input-group__append {
          background: none;
          padding: 0;
          width: 50px;
          display: flex;
          align-items: center;
          justify-content: center;
          box-shadow: none;

          button {
            padding: 0;
            border: none;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;

            i {
              font-size: 20px;
              padding: 10px;
            }

            i:hover {
              background: #ccc;
              color: #fff;
              border-radius: 5px;
            }
          }
        }
      }
    }

    .detail-module {
      height: 100px;
      width: 100%;
      background-color: pink;
      margin: 10px 0;
    }
  }
}
</style>
